package newsSystem.user;

import newsSystem.Order.OrderInfo;
import newsSystem.dao.NewsInfoJDBC;
import newsSystem.dao.OrderInfoJDBC;
import newsSystem.news.NewsInfo;
import newsSystem.news.NewsManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class UserSubJPane extends JFrame {

    private JTextField subscriptionField;
    private JTextField monthField;
    private JTextField dateField;
    private static List<NewsInfo> OList = new ArrayList<>();

    public UserSubJPane(Object[] rowData,String userid) {
        JFrame frame = new JFrame("用户订阅");
        frame.setBounds(10, 10, 400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);//设置空布局，组件想怎么放怎么放
        frame.setLocationRelativeTo(null); // 居中显示


        JLabel subscriptionLabel = new JLabel("订阅份数:");
        JLabel monthLabel = new JLabel("订阅月数:");
        JLabel dateLabel = new JLabel("所需缴纳的金额:");
        subscriptionLabel.setBounds(100, 80, 100, 28);
        monthLabel.setBounds(100, 130, 100, 28);
        dateLabel.setBounds(100, 180, 100, 28);

        subscriptionField = new JTextField(10);
        subscriptionField.setBounds(200, 80, 100, 20);

        monthField = new JTextField(10);
        monthField.setBounds(200, 130, 100, 20);

        dateField = new JTextField(10);
        dateField.setBounds(200, 180, 100, 20);
        dateField.setEditable(false);
        frame.add(subscriptionLabel);
        frame.add(subscriptionField);
        frame.add(monthLabel);
        frame.add(monthField);
        frame.add(dateLabel);
        frame.add(dateField);

        JButton subscribeButton = new JButton("订阅");
        subscribeButton.setBounds(90, 250, 70, 20);
        frame.add(subscribeButton);

        JButton cancelButton = new JButton("取消");
        cancelButton.setBounds(200, 250, 70, 20);
        frame.add(cancelButton);

        // 在这里打印传入的rowData
//        for (int i = 0; i < rowData.length; i++) {
//            System.out.println("rowData[" + i + "]: " + rowData[i]);
//        }

        subscribeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NewsInfoJDBC.newsJDBC();
                List<NewsInfo> newsList = NewsManager.getNewsList();
// 遍历新闻列表
                Object[] result = new Object[0];
                for (NewsInfo news : newsList) {

                    if (news.getNewsID() == (int) rowData[0]) {
                        // 创建一个 Object 数组存储新闻信息
                        result = new Object[]{
                                news.getNewsID(), news.getNewsName(), news.getNewsPublish(), news.getMPrice(), news.getNewsContent(), news.getNewsData(), news.getNewsCNo(), news.getNewsImage(), news.getNewsDept()
                        };
                        break;
                    }
                }
                for (int i = 0; i < result.length; i++) {
                    System.out.println("result[" + i + "]: " + result[i]);
                }


                // Generate random order number
                Random random = new Random();
                int orderNo = random.nextInt(1000);

                // Get current date
                Date currentDate = new Date();

                // Generate random admin ID
//                String[] adminIDs = {"销售一部", "销售二部", "销售三部"};
//                String adminID = adminIDs[random.nextInt(adminIDs.length)];
                String adminID= (String) result[8];

                // Get text from fields
                String subscription = subscriptionField.getText();
                String month = monthField.getText();

                // Calculate order price
//        String subscription = subscriptionField.getText();
                double orderAmount = Double.parseDouble(subscription);
                int orderMonth = Integer.parseInt(month);

// 将 result[3] 转换为 double 类型
                double resultValue = (double) result[3];

                double orderPrice = orderAmount * orderMonth * resultValue;


                String NewsName= (String) rowData[1];

                System.out.println(userid+NewsName+month);
//         Create order object
                OrderInfo order = new OrderInfo(orderNo, subscription, month, String.valueOf(orderPrice), currentDate, userid, adminID, NewsName);
//      输出order的值
                System.out.println("OrderNo: " + order.getOrderNo() + ", OrderAmount: " + order.getOrderAmount() + ", OrderMonth: " + order.getOrderMonth() + ", OrderPrice: " + order.getOrderPrice() + ",OrderDate:" + order.getOrderDate() + ",UserID:" + order.getUserID() + ", 部门名称: " + order.getAdminID() + ",  报刊名称: " + order.getNewsID());

//         Set order price to dateField
                dateField.setText(String.valueOf(orderPrice));
                OrderInfoJDBC.saveOrderToDatabase(order);
                System.out.println("订阅成功");
                // 显示订阅成功的提示框
                JOptionPane.showMessageDialog(null, "订阅成功！", "订阅提示", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取取消按钮所在的窗口
                Window window = SwingUtilities.windowForComponent((Component) e.getSource());

                // 如果找到了窗口，则关闭它
                if (window != null) {
                    window.setVisible(false);
                }
            }
        });
//        frame.pack();
        frame.setVisible(true);
    }
}


//    public static void main(String[] args) {
//        new UserSubJPane();
//    }
//}
